package com.universal.frame.views;


import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.universal.frame.R;
import com.universal.frame.generalutils.NetWorkUtil;
import com.universal.frame.generalutils.ToastUtil;

public class LoadingView extends LinearLayout {

    private Context context;
    private LinearLayout llayProgress;
    private LinearLayout llayNoNetwrok;
    private ImageView imageView;
    private TextView textView;
    private OnInternetLoadingListener mClickListener;
    private Button btnLoading;

    public LoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.layout_loading_view, this, true);
        this.llayProgress = (LinearLayout) findViewById(R.id.llay_progress);
        this.llayNoNetwrok = (LinearLayout) findViewById(R.id.llay_no_netwrok);
        this.btnLoading = (Button) findViewById(R.id.btn_exception_button);
        this.imageView = (ImageView) findViewById(R.id.exception_image);
        this.textView = (TextView) findViewById(R.id.exception_message);

        if (NetWorkUtil.isNetWorkConnected(context)) {
            this.llayProgress.setVisibility(View.VISIBLE);
            this.llayNoNetwrok.setVisibility(View.GONE);
        } else {
            this.llayProgress.setVisibility(View.GONE);
            this.llayNoNetwrok.setVisibility(View.VISIBLE);
        }
    }

    public void showNoNetwrokView() {
        this.llayProgress.setVisibility(View.GONE);
        this.llayNoNetwrok.setVisibility(View.VISIBLE);
    }

    public void showLoadingView() {
        this.llayProgress.setVisibility(View.VISIBLE);
        this.llayNoNetwrok.setVisibility(View.GONE);
    }

    public void setNoDataUi() {
        this.btnLoading.setVisibility(View.GONE);
        this.imageView.setBackgroundResource(R.mipmap.no_data_img);
        this.textView.setText("暂无数据~");
    }

    public void setNoWifiUi() {
        this.btnLoading.setVisibility(View.VISIBLE);
        this.imageView.setBackgroundResource(R.mipmap.icon_cat_cry);
        this.textView.setText("亲，您的网络好像不太好哦~");
    }

    public void setNoEvaluation() {
        this.btnLoading.setVisibility(View.GONE);
        this.imageView.setBackgroundResource(R.mipmap.no_evaluation_img);
        this.textView.setText("暂无评论~");
    }

    public void setIsGone() {
        LoadingView.this.llayProgress.setVisibility(View.VISIBLE);
        LoadingView.this.llayNoNetwrok.setVisibility(View.GONE);
    }

    public void setLoadingClickListener(OnInternetLoadingListener clickListener) {

        this.mClickListener = clickListener;

        if (this.btnLoading != null) {
            this.btnLoading.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    if (!NetWorkUtil.isNetWorkConnected(context)) {
                        ToastUtil.showToast(context,
                                R.string.toast_network_connection_tips);
                        return;
                    }

                    LoadingView.this.llayProgress.setVisibility(View.VISIBLE);
                    LoadingView.this.llayNoNetwrok.setVisibility(View.GONE);
                    if (mClickListener != null) {
                        mClickListener.OnAgainLoading();
                    }


                }
            });
        }

    }

    public interface OnInternetLoadingListener {

        public void OnAgainLoading();
    }


}
